package com.yonyou.hxjy.service.dingtalk.Impl;


import com.yonyou.hxjy.dao.ProcessinstanceMapper;
import com.yonyou.hxjy.entity.ProcessInstance;
import com.yonyou.hxjy.service.dingtalk.Impl.dintalkgetImpl.DingTalkProcessInstanceGetImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DealProcessInstanceData {

    @Autowired
    private DingTalkProcessInstanceGetImpl dingTalkProcessInstanceGetImpl;

    @Autowired
    private ProcessinstanceMapper processinstanceMapper;

    public void saveProcessInstanceId(List<String> processInstanceIdList,String businessType) {
        ProcessInstance processInstance;
        //保存审批单据id到数据库
        for (String processInstanceId : processInstanceIdList) {
            LocalDateTime now = LocalDateTime.now(); // 获取当前日期时间
            Date createDate = new Date();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formattedDate = now.format(formatter);
            processInstance = new ProcessInstance();
            processInstance.setProcessInstanceId(processInstanceId);
            processInstance.setBusinessType(businessType);
            processInstance.setCreateDate(createDate);
            ProcessInstance instance = processinstanceMapper.selectByPrimaryKey(processInstanceId);
            if (instance == null){
                processinstanceMapper.insertSelective(processInstance);
            }else {
                if (instance.getIsSave() == 1){
                    System.out.println("该"+processInstanceId+"单据已保存到ys!");
                }
            }
        }
    }
    public List<String> getProcessInstanceList(String businessType) {
        //根据类型获取未保存的审批单据id
        List<ProcessInstance> processInstances = processinstanceMapper.selectByBusinessType(businessType);
        List<String> processInstanceIdList = new ArrayList<String>();
        for (ProcessInstance processInstance : processInstances) {
            processInstanceIdList.add(processInstance.getProcessInstanceId());
        }
        return processInstanceIdList;
    }
}
